Skip to content

[ty] Model int and str enum value normalization#26349

Merged
charliermarsh merged 24 commits into
mainfrom
charlie/normalize-built-in-enum-mixin-values
Jun 27, 2026
Merged

[ty] Model int and str enum value normalization#26349
charliermarsh merged 24 commits into
mainfrom
charlie/normalize-built-in-enum-mixin-values

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 25, 2026

Copy link
Copy Markdown
Member

Summary

For enums that inherit from int or str, Python converts each assigned value before storing it.

For example:

class Number(int, Enum):
    FALSE = False
    ZERO = 0

Python converts False to 0. Therefore:

  • Number.FALSE.value is 0, not False.
  • Number.ZERO is another name for Number.FALSE, not a separate member.

Previously, we reasoned from the values as written (False and 0) and could incorrectly treat them as different members. We now account for the conversion performed by int or str. And when we can't reliably predict it, we return a less precise type.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 25, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.47%. The percentage of expected errors that received a diagnostic held steady at 89.19%. The number of fully passing files held steady at 95/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 0 7 0
Total 0 7 0

Raw diff:

schemathesis (https://github.com/schemathesis/schemathesis)
- src/schemathesis/generation/overrides.py:75:59 error[invalid-argument-type] Argument to function `lookup_parameter` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:112:73 error[invalid-argument-type] Argument to function `_find_parameter_binding` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:115:97 error[invalid-argument-type] Argument to function `_find_parameter_binding` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:230:21 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:181:9 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:199:9 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:645:38 error[invalid-argument-type] Argument to bound method `list.append` is incorrect: Expected `tuple[str, str]`, found `tuple[Literal["query", "header", "path", "cookie", "body"] | None, str]`

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 87 untouched benchmarks
⏩ 64 skipped benchmarks1


Comparing charlie/normalize-built-in-enum-mixin-values (b271885) with main (df383e1)

Open in CodSpeed

Footnotes

  1. 64 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from e443c06 to 99e6ef0 Compare June 25, 2026 02:01
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch 2 times, most recently from 65f063a to 1599879 Compare June 25, 2026 02:05
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 99e6ef0 to 70a659b Compare June 25, 2026 02:05
@charliermarsh charliermarsh marked this pull request as ready for review June 25, 2026 22:58
@charliermarsh charliermarsh requested a review from a team as a code owner June 25, 2026 22:58
@astral-sh-bot astral-sh-bot Bot requested a review from sharkdp June 25, 2026 22:58
@charliermarsh charliermarsh added the bug Something isn't working label Jun 25, 2026
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch from 1599879 to 6ce7e33 Compare June 25, 2026 23:02
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 70a659b to 579b61b Compare June 25, 2026 23:03
Base automatically changed from charlie/fix-enum-value-metadata to main June 25, 2026 23:14
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 579b61b to 193d068 Compare June 25, 2026 23:14
@charliermarsh charliermarsh marked this pull request as draft June 25, 2026 23:22
@charliermarsh charliermarsh changed the title [ty] Normalize built-in enum mixin values [ty] Model int and str enum value normalization Jun 26, 2026
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 2f27190 to 532e2cd Compare June 26, 2026 00:49
@charliermarsh charliermarsh removed the request for review from sharkdp June 26, 2026 00:49
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch 3 times, most recently from 146ef8b to 80c7281 Compare June 26, 2026 14:39
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 3fed494 to a4f16cb Compare June 26, 2026 15:27
@charliermarsh charliermarsh changed the base branch from main to charlie/detect-inherited-enum-new-member June 26, 2026 15:27
@charliermarsh charliermarsh marked this pull request as ready for review June 26, 2026 15:51
@astral-sh-bot astral-sh-bot Bot requested a review from sharkdp June 26, 2026 15:51
charliermarsh added a commit that referenced this pull request Jun 27, 2026
## Summary

When creating an enum class, CPython checks an inherited
`__new_member__` before falling back to an inherited `__new__` or the
data-type constructor. We previously skipped that lookup, so a parent
enum could replace `_value_` while we continued to expose the member's
declared right-hand side as its precise `.value` type.

This change includes inherited `__new_member__` in enum constructor
discovery. When that hook is user-defined, member values now use the
same conservative inference as other custom construction paths:

```python
from enum import Enum

class Base(Enum):
    def __new_member__(cls: type["Base"], value: int) -> "Base":
        obj = object.__new__(cls)
        obj._value_ = str(value)
        return obj

class Child(Base):
    VALUE = 1

reveal_type(Child.VALUE.value)  # Any
```

This is split out from #26349 so constructor discovery can land
independently of built-in enum mixin normalization.
Base automatically changed from charlie/detect-inherited-enum-new-member to main June 27, 2026 02:45
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from a4f16cb to 9c736d1 Compare June 27, 2026 03:19
@charliermarsh charliermarsh enabled auto-merge (squash) June 27, 2026 03:48
@charliermarsh charliermarsh merged commit 03f787e into main Jun 27, 2026
62 checks passed
@charliermarsh charliermarsh deleted the charlie/normalize-built-in-enum-mixin-values branch June 27, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants